package com.c201801020208.ybl.Adapters;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.c201801020208.ybl.Company.Activity.JobInfoActivity;
import com.c201801020208.ybl.R;
import com.c201801020208.ybl.Tables.UserInfo;
import com.c201801020208.ybl.User.Activity.jianliActivity;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class HomePageAdapter extends RecyclerView.Adapter<HomePageAdapter.ViewHolder> {

    String type;
    List<UserInfo> mList = new ArrayList<>();

    public HomePageAdapter(Context mContext, String type) {
        this.type = type;
        BmobQuery<UserInfo> categoryBmobQuery = new BmobQuery<>();
        categoryBmobQuery.addWhereEqualTo("type", type);
        categoryBmobQuery.findObjects(new FindListener<UserInfo>() {
            @Override
            public void done(List<UserInfo> object, BmobException e) {
                if (e == null) {
//                    Toast.makeText(mContext, "查询成功：" + object.size(), Toast.LENGTH_LONG).show();
                    for (int i = 0; i < object.size(); i++) {
                        UserInfo userInfo = new UserInfo();
                        userInfo.setUsername(object.get(i).getUsername());
                        userInfo.setAddress(object.get(i).getAddress());
                        userInfo.setAge(object.get(i).getAge());
                        userInfo.setSex(object.get(i).getSex());
                        userInfo.setWorkexperience(object.get(i).getWorkexperience());
                        userInfo.setSalary(object.get(i).getSalary());
                        userInfo.setSchool(object.get(i).getSchool());
                        userInfo.setWantedjob(object.get(i).getWantedjob());
                        userInfo.setMajor(object.get(i).getMajor());
                        userInfo.setDescribe(object.get(i).getDescribe());
                        userInfo.setProjectexperience(object.get(i).getProjectexperience());
                        mList.add(userInfo);
                    }
                    notifyDataSetChanged();
                } else {
                    Log.e("BMOB", e.toString());
//                    Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.usersresume, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        UserInfo userInfo = mList.get(position);
        holder.address.setText(userInfo.getAddress());
        holder.school.setText(userInfo.getSchool());
        holder.wantedjob.setText(userInfo.getWantedjob());
        holder.age.setText(userInfo.getAge());
        holder.workexperience.setText(userInfo.getWorkexperience());
//        holder.discribe.setText(userInfo.getDescribe());
        holder.salary.setText(userInfo.getSalary());
        holder.sex.setText(userInfo.getSex());
//        holder.projectexperience.setText(userInfo.getProjectexperience());
        holder.username.setText(userInfo.getUsername());
        holder.major.setText(userInfo.getMajor());

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), jianliActivity.class);
                intent.putExtra("userInfo", userInfo);
                v.getContext().startActivity(intent);
            }
        });
//       holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
//           @Override
//           public boolean onLongClick(View v) {
//               new AlertDialog.Builder(v.getContext())
//                       .setMessage("确定删除这条招聘信息吗？")
//                       .setCancelable(false)
//                       .setPositiveButton("是", new DialogInterface.OnClickListener() {
//                           @Override
//                           public void onClick(DialogInterface dialog, int which) {
//                                 mList.remove(position);
//                           }
//                       })
//                       .setNegativeButton("否", new DialogInterface.OnClickListener() {
//                           @Override
//                           public void onClick(DialogInterface dialog, int which) {
//                               dialog.dismiss();
//                           }
//                       }).create().show();
//               return false;
//           }
//       });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        TextView school;
        TextView projectexperience;
        TextView salary;
        TextView username;
        TextView address;
        TextView age;
        TextView wantedjob;
        TextView sex;
        TextView workexperience;
        TextView discribe;
        TextView major;


        public ViewHolder(View view) {
            super(view);
            mView = view;
            school = view.findViewById(R.id.textView13);
//            projectexperience=view.findViewById(R.id.textView31);
            salary = view.findViewById(R.id.textView14);
            username = view.findViewById(R.id.textView9);
            address = view.findViewById(R.id.textView8);
            age = view.findViewById(R.id.textView15);
            wantedjob = view.findViewById(R.id.textView17);
            sex = view.findViewById(R.id.textView16);
            workexperience = view.findViewById(R.id.textView27);
//            discribe=view.findViewById(R.id.textView33);
            major = view.findViewById(R.id.textView28);
        }
    }
}

